home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / components / nsFilePicker.js < prev    next >
Text File  |  2006-05-08  |  12KB  |  345 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 2000
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Stuart Parmenter <pavlov@netscape.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  27.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /*
  40.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  41.  * If you must perform some initialization, and it could possibly fail (even
  42.  * due to an out-of-memory condition), you should use an Init method, which
  43.  * can convey failure appropriately (thrown exception in JS,
  44.  * NS_FAILED(nsresult) return in C++).
  45.  *
  46.  * In JS, you can actually cheat, because a thrown exception will cause the
  47.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  48.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  49.  * for portability reasons -- and even when you're building completely
  50.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  51.  */
  52.  
  53.  
  54. const DEBUG = false; /* set to true to enable debug messages */
  55.  
  56. const FILEPICKER_CONTRACTID     = "@mozilla.org/filepicker;1";
  57. const FILEPICKER_CID        = Components.ID("{54ae32f8-1dd2-11b2-a209-df7c505370f8}");
  58. const LOCAL_FILE_CONTRACTID = "@mozilla.org/file/local;1";
  59. const APPSHELL_SERV_CONTRACTID  = "@mozilla.org/appshell/appShellService;1";
  60. const STRBUNDLE_SERV_CONTRACTID = "@mozilla.org/intl/stringbundle;1";
  61.  
  62. const nsIAppShellService    = Components.interfaces.nsIAppShellService;
  63. const nsILocalFile          = Components.interfaces.nsILocalFile;
  64. const nsIFileURL            = Components.interfaces.nsIFileURL;
  65. const nsISupports           = Components.interfaces.nsISupports;
  66. const nsIFactory            = Components.interfaces.nsIFactory;
  67. const nsIFilePicker         = Components.interfaces.nsIFilePicker;
  68. const nsIInterfaceRequestor = Components.interfaces.nsIInterfaceRequestor
  69. const nsIDOMWindow          = Components.interfaces.nsIDOMWindow;
  70. const nsIStringBundleService = Components.interfaces.nsIStringBundleService;
  71. const nsIWebNavigation      = Components.interfaces.nsIWebNavigation;
  72. const nsIDocShellTreeItem   = Components.interfaces.nsIDocShellTreeItem;
  73. const nsIBaseWindow         = Components.interfaces.nsIBaseWindow;
  74.  
  75. var   bundle                = null;
  76. var   lastDirectory         = null;
  77.  
  78. function nsFilePicker()
  79. {
  80.   if (!bundle)
  81.     bundle = srGetStrBundle("chrome://global/locale/filepicker.properties");
  82.  
  83.   /* attributes */
  84.   this.mDefaultString = "";
  85.   this.mFilterIndex = 0;
  86.   this.mFilterTitles = new Array();
  87.   this.mFilters = new Array();
  88.   this.mDisplayDirectory = null;
  89.   if (lastDirectory) {
  90.     try {
  91.       var dir = Components.classes[LOCAL_FILE_CONTRACTID].createInstance(nsILocalFile);
  92.       dir.initWithPath(lastDirectory);
  93.       this.mDisplayDirectory = dir;
  94.     } catch (e) {}
  95.   }
  96. }
  97.  
  98. nsFilePicker.prototype = {
  99.  
  100.   /* attribute nsILocalFile displayDirectory; */
  101.   set displayDirectory(a) {
  102.     this.mDisplayDirectory = a &&
  103.       a.clone().QueryInterface(nsILocalFile);
  104.   },
  105.   get displayDirectory()  {
  106.     return this.mDisplayDirectory &&
  107.            this.mDisplayDirectory.clone()
  108.                .QueryInterface(nsILocalFile);
  109.   },
  110.  
  111.   /* readonly attribute nsILocalFile file; */
  112.   set file(a) { throw "readonly property"; },
  113.   get file()  { return this.mFilesEnumerator.mFiles[0]; },
  114.  
  115.   /* readonly attribute nsISimpleEnumerator files; */
  116.   set files(a) { throw "readonly property"; },
  117.   get files()  { return this.mFilesEnumerator; },
  118.  
  119.   /* readonly attribute nsIFileURL fileURL; */
  120.   set fileURL(a) { throw "readonly property"; },
  121.   get fileURL()  {
  122.     if (this.mFilesEnumerator) {
  123.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  124.                     .getService(Components.interfaces.nsIIOService);
  125.       var url       = ioService.newFileURI(this.file);
  126.       return url;
  127.     }
  128.     return null;
  129.   },
  130.  
  131.   /* attribute wstring defaultString; */
  132.   set defaultString(a) { this.mDefaultString = a; },
  133.   get defaultString()  { return this.mDefaultString; },
  134.  
  135.   /* attribute wstring defaultExtension */
  136.   set defaultExtension(ext) { },
  137.   get defaultExtension() { return ""; },
  138.  
  139.   /* attribute long filterIndex; */
  140.   set filterIndex(a) { this.mFilterIndex = a; },
  141.   get filterIndex() { return this.mFilterIndex; },
  142.  
  143.   /* members */
  144.   mFilesEnumerator: undefined,
  145.   mParentWindow: null,
  146.  
  147.   /* methods */
  148.   init: function(parent, title, mode) {
  149.     this.mParentWindow = parent;
  150.     this.mTitle = title;
  151.     this.mMode = mode;
  152.   },
  153.  
  154.   appendFilters: function(filterMask) {
  155.     if (filterMask & nsIFilePicker.filterHTML) {
  156.       this.appendFilter(bundle.GetStringFromName("htmlTitle"),
  157.                    bundle.GetStringFromName("htmlFilter"));
  158.     }
  159.     if (filterMask & nsIFilePicker.filterText) {
  160.       this.appendFilter(bundle.GetStringFromName("textTitle"),
  161.                    bundle.GetStringFromName("textFilter"));
  162.     }
  163.     if (filterMask & nsIFilePicker.filterImages) {
  164.       this.appendFilter(bundle.GetStringFromName("imageTitle"),
  165.                    bundle.GetStringFromName("imageFilter"));
  166.     }
  167.     if (filterMask & nsIFilePicker.filterXML) {
  168.       this.appendFilter(bundle.GetStringFromName("xmlTitle"),
  169.                    bundle.GetStringFromName("xmlFilter"));
  170.     }
  171.     if (filterMask & nsIFilePicker.filterXUL) {
  172.       this.appendFilter(bundle.GetStringFromName("xulTitle"),
  173.                    bundle.GetStringFromName("xulFilter"));
  174.     }
  175.     if (filterMask & nsIFilePicker.filterApps) {
  176.       // We use "..apps" as a special filter for executable files
  177.       this.appendFilter(bundle.GetStringFromName("appsTitle"),
  178.                         "..apps");
  179.     }
  180.     if (filterMask & nsIFilePicker.filterAll) {
  181.       this.appendFilter(bundle.GetStringFromName("allTitle"),
  182.                    bundle.GetStringFromName("allFilter"));
  183.     }
  184.   },
  185.  
  186.   appendFilter: function(title, extensions) {
  187.     this.mFilterTitles.push(title);
  188.     this.mFilters.push(extensions);
  189.   },
  190.  
  191.   QueryInterface: function(iid) {
  192.     if (iid.equals(nsIFilePicker) ||
  193.         iid.equals(nsISupports))
  194.       return this;
  195.  
  196.     Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  197.     return null;
  198.   },
  199.  
  200.   show: function() {
  201.     var o = new Object();
  202.     o.title = this.mTitle;
  203.     o.mode = this.mMode;
  204.     o.displayDirectory = this.mDisplayDirectory;
  205.     o.defaultString = this.mDefaultString;
  206.     o.filterIndex = this.mFilterIndex;
  207.     o.filters = new Object();
  208.     o.filters.titles = this.mFilterTitles;
  209.     o.filters.types = this.mFilters;
  210.     o.retvals = new Object();
  211.  
  212.     var parent;
  213.     if (this.mParentWindow) {
  214.       parent = this.mParentWindow;
  215.     } else if (typeof(window) == "object" && window != null) {
  216.       parent = window;
  217.     } else {
  218.       try {
  219.         var appShellService = Components.classes[APPSHELL_SERV_CONTRACTID].getService(nsIAppShellService);
  220.         parent = appShellService.hiddenDOMWindow;
  221.       } catch(ex) {
  222.         debug("Can't get parent.  xpconnect hates me so we can't get one from the appShellService.\n");
  223.         debug(ex + "\n");
  224.       }
  225.     }
  226.  
  227.     var parentWin = null;
  228.     try {
  229.       parentWin = parent.QueryInterface(nsIInterfaceRequestor)
  230.                         .getInterface(nsIWebNavigation)
  231.                         .QueryInterface(nsIDocShellTreeItem)
  232.                         .treeOwner
  233.                         .QueryInterface(nsIInterfaceRequestor)
  234.                         .getInterface(nsIBaseWindow);
  235.     } catch(ex) {
  236.       dump("file picker couldn't get base window\n"+ex+"\n");
  237.     }
  238.     try {
  239.       if (parentWin)
  240.         parentWin.blurSuppression = true;
  241.       parent.openDialog("chrome://global/content/filepicker.xul",
  242.                         "",
  243.                         "chrome,modal,titlebar,resizable=yes,dependent=yes",
  244.                         o);
  245.       if (parentWin)
  246.         parentWin.blurSuppression = false;
  247.  
  248.       this.mFilterIndex = o.retvals.filterIndex;
  249.       this.mFilesEnumerator = o.retvals.files;
  250.       lastDirectory = o.retvals.directory;
  251.       return o.retvals.buttonStatus;
  252.     } catch(ex) { dump("unable to open file picker\n" + ex + "\n"); }
  253.  
  254.     return null;
  255.   }
  256. }
  257.  
  258. if (DEBUG)
  259.     debug = function (s) { dump("-*- filepicker: " + s + "\n"); }
  260. else
  261.     debug = function (s) {}
  262.  
  263. /* module foo */
  264.  
  265. var filePickerModule = new Object();
  266.  
  267. filePickerModule.registerSelf =
  268. function (compMgr, fileSpec, location, type)
  269. {
  270.     debug("registering (all right -- a JavaScript module!)");
  271.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  272.  
  273.     compMgr.registerFactoryLocation(FILEPICKER_CID,
  274.                                     "FilePicker JS Component",
  275. //@line 278 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/xpfe/components/filepicker/src/nsFilePicker.js.in"
  276.                                     "",
  277. //@line 280 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/xpfe/components/filepicker/src/nsFilePicker.js.in"
  278.                                     fileSpec,
  279.                                     location,
  280.                                     type);
  281. }
  282.  
  283. filePickerModule.getClassObject =
  284. function (compMgr, cid, iid) {
  285.     if (!cid.equals(FILEPICKER_CID))
  286.         throw Components.results.NS_ERROR_NO_INTERFACE;
  287.  
  288.     if (!iid.equals(Components.interfaces.nsIFactory))
  289.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  290.  
  291.     return filePickerFactory;
  292. }
  293.  
  294. filePickerModule.canUnload =
  295. function(compMgr)
  296. {
  297.     debug("Unloading component.");
  298.     return true;
  299. }
  300.  
  301. /* factory object */
  302. var filePickerFactory = new Object();
  303.  
  304. filePickerFactory.createInstance =
  305. function (outer, iid) {
  306.     debug("CI: " + iid);
  307.     debug("IID:" + nsIFilePicker);
  308.     if (outer != null)
  309.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  310.  
  311.     return (new nsFilePicker()).QueryInterface(iid);
  312. }
  313.  
  314. /* entrypoint */
  315. function NSGetModule(compMgr, fileSpec) {
  316.     return filePickerModule;
  317. }
  318.  
  319.  
  320.  
  321. /* crap from strres.js that I want to use for string bundles since I can't include another .js file.... */
  322.  
  323. var strBundleService = null;
  324.  
  325. function srGetStrBundle(path)
  326. {
  327.   var strBundle = null;
  328.  
  329.   if (!strBundleService) {
  330.     try {
  331.       strBundleService = Components.classes[STRBUNDLE_SERV_CONTRACTID].getService(nsIStringBundleService);
  332.     } catch (ex) {
  333.       dump("\n--** strBundleService createInstance failed **--\n");
  334.       return null;
  335.     }
  336.   }
  337.  
  338.   strBundle = strBundleService.createBundle(path);
  339.   if (!strBundle) {
  340.     dump("\n--** strBundle createInstance failed **--\n");
  341.   }
  342.   return strBundle;
  343. }
  344.  
  345.